<%@page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<script>
	$(function() {
		$("#positionHistory").combotree({
			url : $.webapp.root+"/static_res/position.tree.json",
			idFiled:'pid', textFiled:'name', editable: false, required:false, lines:true,required:true,
			onSelect: function(a) {
				$("input[name=positionName]").val(a.name);
			}
		});
		
		$.post($.webapp.root+"/admin/system/person/get.do", {id: "${personId}"}, function(result) {
			$("#curPosition").html(result.position_name);
		}, 'json').error(function(){$.easyui.loaded();});
		
		$("#startDate").val(new Date().format('yyyy-MM-dd')) ;
		$("#endDate").val(new Date().format('yyyy-MM-dd')) ;
		
		loadHistory();
	});
	
	function loadHistory() {
		//编辑，加载表单数据
		$.post("${ctx}/admin/sys/position_history/list.do", {"personId": "${personId}"}, function(result) {
			var tbody = $("#historyList");	
			tbody.empty();
			if(null != result && result.length > 0) {
				tbody.empty();
				$.each(result, function(i, item) {
					var tr = $("<tr></tr>").appendTo(tbody);
					var td1 = $("<th></th>").appendTo(tr).html("变更日期：");
					var td2 = $("<td></td>").appendTo(tr).html($.date.format(item.created, "yyyy-MM-dd"));
					var td5 = $("<th></th>").appendTo(tr).html("岗位：");
					var td6 = $("<td></td>").css("width", "140px").appendTo(tr).html(item.positionName);
					var td1 = $("<th></th>").appendTo(tr).html("开始日期：");
					var td2 = $("<td></td>").appendTo(tr).html($.date.format(item.startDate, "yyyy-MM-dd"));
					var td3 = $("<th></th>").appendTo(tr).html("结束日期：");
					var td4 = $("<td></td>").appendTo(tr).html($.date.format(item.endDate, "yyyy-MM-dd"));
					var td7 = $("<td></td>").appendTo(tr).html($.string.format("<a href='javascript:delPosition(\"{0}\");'>{1}</a>", item.id, "删除"));
				});
			}
		}, 'json').error(function(){$.easyui.loaded();});
	}
	
	function delPosition(id) {
		$.easyui.loading({ msg: "数据删除中，请稍等..."});
		$.post($.webapp.root+"/admin/sys/position_history/delete.do", {"id" : id}, function(result) {
			if (result.status) {
				alertify.success(result.msg);
				$.easyui.loaded();
				loadHistory();
			} else {
				alertify.warning(result.msg);
				$.easyui.loaded();
			}
		}, 'json').error(function() { $.easyui.loaded(); });
	}
	
	//提交表单数据
	var submitNow = function($d, flag) {
		var data = $("#form").form("getData") ;
		if(data.startDate == null || data.startDate == '') {
			$.easyui.loaded(); $.easyui.loaded();$.messager.alert("提示", "请填写开始日期", "info");
			return ;
		}
		if(data.endDate == null || data.endDate == '') {
			$.easyui.loaded(); $.easyui.loaded();$.messager.alert("提示", "请填写结束日期", "info");
			return ;
		}
		$.post("${ctx}/admin/sys/position_history/add.do", $("#form").form("getData"), function(result) {
			if (result.status) {
				$.easyui.loaded(); 
				alertify.success(result.msg);
				loadHistory();
				if(flag==undefined||flag==false){$d.dialog("close") ;}
			} else {
				$.easyui.loaded();$.messager.alert("错误", result.msg, "error");
			}
		}, 'json').error(function(){$.easyui.loaded();});
	};
	
	//验证表单
	var submitForm = function($d, flag) {
		if($('#form').form('validate')) {
			$.easyui.loading({ msg: "数据提交中，请稍等..." });
			submitNow($d, flag) ;
		} 
	};
</script>

<form id="form" class="easyui-form form_container">
	<input type="hidden" name="id" value="${id}" />
	<input type="hidden" name="modifyName" value="${USER_SESSION.user.emp_name}" />
	<input type="hidden" name="personId" value="${personId}" />
	<input type="hidden" name="personName" value="${personName}" />
	<input type="hidden" name="positionName" value="" />
	<table class="tableform projectry">
		<tr>
			<th colspan="6" style="text-align: left;">
				&nbsp;当前岗位：<span id="curPosition"></span>
			</th>
		</tr>
		<tr>
			<th>开始日期：</th>
			<td>
				<input id="startDate" name="startDate" value="" class="Wdate" style="height:23px;width:102px;" type="text" onFocus="WdatePicker({
										isShowClear:true,
										readOnly:true
										})"/>
			</td>
			<th>结束日期：</th>
			<td>
				<input id="endDate" name="endDate" value="" class="Wdate" style="height:23px;width:102px;" type="text" onFocus="WdatePicker({
										isShowClear:true,
										readOnly:true
										})"/>
			</td>
			<th>岗位：</th>
			<td>
				<input id="positionHistory" name="positionId" style="width:180px;height:25px;" />
			</td>
		</tr>
	</table>
	<br>
	<table class="tableform projectry">
		<thead>
			<tr>
				<th colspan="9" style="text-align: left;">
					历史岗位
				</th>
			</tr>
		</thead>
		
		<tbody id="historyList">
		</tbody>
		
	</table>
</form>